package profitshare

import (
	. "wxsdk/wxpay"
	// "errors"
	"wxsdk/wxpay/core"
	"wxsdk/wxpay/utils/mapstruct"
	//"github.com/astaxie/beego"
)

//1.定义结构体，存储查询订单号使用的参数,注意：out_trade_no和transaction_id必须填一个，2个都存在的情况下，优先以transaction_id为准
type ProfitQueryReq struct {
	MchId         string `xml:"mch_id"`                   //mch_id
	SubMchId      string `xml:"sub_mch_id,omitempty"`     //微信支付分配的子商户号(服务商特有参数)
	TransactionId string `xml:"transaction_id,omitempty"` //微信订单号
	OutOrderNo    string `xml:"out_order_no,omitempty"`   //商户订单号
	NonceStr      string `xml:"nonce_str"`                //随机字符串
	Sign          string `xml:"sign"`                     //签名
	SignType      string `xml:"sign_type,omitempty"`      //签名类型
}

// 查询订单
type ProfitQueryRsp struct {
	BaseResponse  `xml:",innerXml,omitempty"`
	TransactionId string `xml:"transaction_id,omitempty"` // 微信支付订单号
	OutOrderNo    string `xml:"out_order_no,omitempty"`   // 商户订单号
	OrderId       string `xml:"order_id,omitempty"`       //
	Status        string `xml:"status,omitempty"`         //
	CloseReason   string `xml:"close_reason,omitempty"`   //
	Receivers     string `xml:"receivers,omitempty"`      //
	Amount        int    `xml:"amount"`                   //
	Description   string `xml:"description"`              //
}

func OrderQuery(appid, mchId, subMchId, appSecret, transactionId, orderIdStr string) (*ProfitQueryRsp, error) {
	var (
		req           = new(ProfitQueryReq)
		rsp           = new(ProfitQueryRsp)
		ReqUrl string = "https://api.mch.weixin.qq.com/pay/profitsharingquery"
	)
	client := core.GetWxPayClient(appid, mchId, appSecret, nil)

	req.MchId = mchId
	if len(subMchId) > 0 {
		req.SubMchId = subMchId
	}
	req.TransactionId = transactionId
	req.OutOrderNo = orderIdStr //商户订单号
	rspMap, err := client.ExcuteMap(ReqUrl, req)
	if err != nil {
		return rsp, err
	}
	err = mapstruct.Map2Struct(rspMap, rsp, "xml")
	if err != nil {
		return rsp, err
	}
	return rsp, nil
}
